package flc.ast.activity;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.stark.calculator.unit.constant.ConvertType;
import com.stark.calculator.unit.model.UnitBean;
import com.stark.calculator.unit.model.UnitDataProvider;

import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.databinding.ActivityCaloriesBinding;
import stark.common.basic.utils.Str2NumUtil;

public class CaloriesActivity extends BaseAc<ActivityCaloriesBinding> {

    String str = "1.0";
    private UnitBean mBaseUnitBean, newUnitbean;
    private List<UnitBean> mAllUnitBean;
    private double mInputValue = 1;


    @Override
    protected int onCreate() {
        return R.layout.activity_calories;
    }

    @Override
    protected void initView() {
        mDataBinding.ivCaloriesBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        mDataBinding.ivCaloriesClear.setOnClickListener(this);
        mDataBinding.ivCaloriesDian.setOnClickListener(this);
        mDataBinding.ivCaloriesNum0.setOnClickListener(this);
        mDataBinding.ivCaloriesNum1.setOnClickListener(this);
        mDataBinding.ivCaloriesNum2.setOnClickListener(this);
        mDataBinding.ivCaloriesNum3.setOnClickListener(this);
        mDataBinding.ivCaloriesNum4.setOnClickListener(this);
        mDataBinding.ivCaloriesNum5.setOnClickListener(this);
        mDataBinding.ivCaloriesNum6.setOnClickListener(this);
        mDataBinding.ivCaloriesNum7.setOnClickListener(this);
        mDataBinding.ivCaloriesNum8.setOnClickListener(this);
        mDataBinding.ivCaloriesNum9.setOnClickListener(this);
        mDataBinding.llCaloriesMore1.setOnClickListener(this);
        mDataBinding.llCaloriesMore2.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivCaloriesClear:
                str = "";
                mDataBinding.tvCaloriesNum1.setText(str);
                mDataBinding.tvCaloriesNum2.setText(str);
                break;
            case R.id.ivCaloriesNum0:
                if (str.length() == 0) {
                    return;
                }
                str += "0";
                mDataBinding.tvCaloriesNum1.setText(str);
                mInputValue = Str2NumUtil.parse(str);
                updateNewUnitView();
                break;
            case R.id.ivCaloriesNum1:
                str += "1";
                mDataBinding.tvCaloriesNum1.setText(str);
                mInputValue = Str2NumUtil.parse(str);
                updateNewUnitView();
                break;
            case R.id.ivCaloriesNum2:
                str += "2";
                mDataBinding.tvCaloriesNum1.setText(str);
                mInputValue = Str2NumUtil.parse(str);
                updateNewUnitView();
                break;
            case R.id.ivCaloriesNum3:
                str += "3";
                mDataBinding.tvCaloriesNum1.setText(str);
                mInputValue = Str2NumUtil.parse(str);
                updateNewUnitView();
                break;
            case R.id.ivCaloriesNum4:
                str += "4";
                mDataBinding.tvCaloriesNum1.setText(str);
                mInputValue = Str2NumUtil.parse(str);
                updateNewUnitView();
                break;
            case R.id.ivCaloriesNum5:
                str += "5";
                mDataBinding.tvCaloriesNum1.setText(str);
                mInputValue = Str2NumUtil.parse(str);
                updateNewUnitView();
                break;
            case R.id.ivCaloriesNum6:
                str += "6";
                mDataBinding.tvCaloriesNum1.setText(str);
                mInputValue = Str2NumUtil.parse(str);
                updateNewUnitView();
                break;
            case R.id.ivCaloriesNum7:
                str += "7";
                mDataBinding.tvCaloriesNum1.setText(str);
                mInputValue = Str2NumUtil.parse(str);
                updateNewUnitView();
                break;
            case R.id.ivCaloriesNum8:
                str += "8";
                mDataBinding.tvCaloriesNum1.setText(str);
                mInputValue = Str2NumUtil.parse(str);
                updateNewUnitView();
                break;
            case R.id.ivCaloriesNum9:
                str += "9";
                mDataBinding.tvCaloriesNum1.setText(str);
                mInputValue = Str2NumUtil.parse(str);
                updateNewUnitView();
                break;
            case R.id.ivCaloriesDian:
                if (str.contains(".")) {
                    return;
                }
                str += ".";
                mDataBinding.tvCaloriesNum1.setText(str);
                mInputValue = Str2NumUtil.parse(str);
                updateNewUnitView();
                break;
            case R.id.llCaloriesMore1:
                Intent intent = new Intent(CaloriesActivity.this, SelLengthActivity.class);
                intent.putExtra("selName", mDataBinding.tvCaloriesType1.getText().toString());
                SelLengthActivity.convertType = ConvertType.CALORIES;
                startActivityForResult(intent, 11);
                break;
            case R.id.llCaloriesMore2:
                Intent intent1 = new Intent(CaloriesActivity.this, SelLengthActivity.class);
                intent1.putExtra("selName", mDataBinding.tvCaloriesType2.getText().toString());
                SelLengthActivity.convertType = ConvertType.CALORIES;
                startActivityForResult(intent1, 12);
                break;
        }
    }

    @Override
    protected void initData() {
        updateBaseUnitView();
        updateNewUnitView();
    }

    private void updateBaseUnitView() {
        if (mBaseUnitBean == null) {
            mBaseUnitBean = UnitDataProvider.getBaseUnitBean(ConvertType.CALORIES);
        }
        str = mBaseUnitBean.value;
        mDataBinding.tvCaloriesType1.setText(mBaseUnitBean.cnName);
        mDataBinding.tvCaloriesNum1.setText(mBaseUnitBean.value);
        mDataBinding.tvCaloriesUnit1.setText(mBaseUnitBean.enName);
    }

    private void updateNewUnitView() {
        if (mAllUnitBean == null) {
            mAllUnitBean = UnitDataProvider.getDatas(ConvertType.CALORIES);
        }
        for (UnitBean bean : mAllUnitBean) {
            if (bean.cnName.equals(mDataBinding.tvCaloriesType2.getText().toString())) {
                double newValue = ((1 * bean.baseRateValue) / mBaseUnitBean.baseRateValue) * mInputValue;
                bean.value = String.valueOf(newValue);
                mDataBinding.tvCaloriesType2.setText(bean.cnName);
                mDataBinding.tvCaloriesNum2.setText(bean.value);
                mDataBinding.tvCaloriesUnit2.setText(bean.enName);
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == 11) {
                String selName = data.getStringExtra("selName");
                for (UnitBean bean : mAllUnitBean) {
                    if (bean.cnName.equals(selName)) {
                        mBaseUnitBean = bean;
                        mBaseUnitBean.value = String.valueOf(1.0);
                    }
                }
                mInputValue = 1;
                updateBaseUnitView();
                updateNewUnitView();
            } else if (requestCode == 12) {
                String selName = data.getStringExtra("selName");
                mDataBinding.tvCaloriesType2.setText(selName);
                updateNewUnitView();
            }
        }
    }
}